home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0192.ARJ / VIDEO.C < prev    next >
Text File  |  1991-10-22  |  6KB  |  228 lines

  1. /* --------------------- video.c -------------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. int ClipString;
  6.  
  7. static unsigned video_address;
  8. /* -- read a rectangle of video memory into a save buffer -- */
  9. void getvideo(RECT rc, void far *bf)
  10. {
  11.     int ht = RectBottom(rc)-RectTop(rc)+1;
  12.     int bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  13.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  14.     hide_mousecursor();
  15.     while (ht--)    {
  16.         movedata(video_address, vadr, FP_SEG(bf),
  17.                 FP_OFF(bf), bytes_row);
  18.         vadr += SCREENWIDTH*2;
  19.         bf = (char far *)bf + bytes_row;
  20.     }
  21.     show_mousecursor();
  22. }
  23.  
  24. /* -- write a rectangle of video memory from a save buffer -- */
  25. void storevideo(RECT rc, void far *bf)
  26. {
  27.     int ht = RectBottom(rc)-RectTop(rc)+1;
  28.     int bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  29.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  30.     hide_mousecursor();
  31.     while (ht--)    {
  32.         movedata(FP_SEG(bf), FP_OFF(bf), video_address,
  33.                 vadr, bytes_row);
  34.         vadr += SCREENWIDTH*2;
  35.         bf = (char far *)bf + bytes_row;
  36.     }
  37.     show_mousecursor();
  38. }
  39.  
  40. /* -------- read a character of video memory ------- */
  41. unsigned int GetVideoChar(int x, int y)
  42. {
  43.     int c;
  44.     hide_mousecursor();
  45.     c = peek(video_address, vad(x,y));
  46.     show_mousecursor();
  47.     return c;
  48. }
  49.  
  50. /* -------- write a character of video memory ------- */
  51. void PutVideoChar(int x, int y, int c)
  52. {
  53.     if (x < SCREENWIDTH && y < SCREENHEIGHT)    {
  54.         hide_mousecursor();
  55.         poke(video_address, vad(x,y), c);
  56.         show_mousecursor();
  57.     }
  58. }
  59.  
  60. static int isAncestor(WINDOW wnd, WINDOW awnd)
  61. {
  62.     while (wnd != NULL)    {
  63.         if (wnd == awnd)
  64.             return TRUE;
  65.         wnd = GetParent(wnd);
  66.     }
  67.     return FALSE;
  68. }
  69.  
  70. int CharInView(WINDOW wnd, int x, int y)
  71. {
  72.     WINDOW nwnd = NextWindow(wnd);
  73.     WINDOW pwnd;
  74.     RECT rc;
  75.     int x1 = GetLeft(wnd)+x;
  76.     int y1 = GetTop(wnd)+y;
  77.  
  78.     if (!TestAttribute(wnd, VISIBLE))
  79.         return FALSE;
  80.     if (!TestAttribute(wnd, NOCLIP))    {
  81.         WINDOW wnd1 = GetParent(wnd);
  82.         while (wnd1 != NULL)    {
  83.             /* --- clip character to parent's borders -- */
  84.             if (!TestAttribute(wnd1, VISIBLE))
  85.                 return FALSE;
  86.             if (!InsideRect(x1, y1, ClientRect(wnd1)))
  87.                 return FALSE;
  88.             wnd1 = GetParent(wnd1);
  89.         }
  90.     }
  91.     while (nwnd != NULL)    {
  92.         if (isVisible(nwnd) && !isAncestor(wnd, nwnd))    {
  93.             rc = WindowRect(nwnd);
  94.             if (!TestAttribute(nwnd, NOCLIP))    {
  95.                 pwnd = nwnd;
  96.                 while (GetParent(pwnd))    {
  97.                     pwnd = GetParent(pwnd);
  98.                     rc = subRectangle(rc, ClientRect(pwnd));
  99.                 }
  100.             }
  101.             if (InsideRect(x1,y1,rc))
  102.                 return FALSE;
  103.         }
  104.         nwnd = NextWindow(nwnd);
  105.     }
  106.     return (x1 < SCREENWIDTH && y1 < SCREENHEIGHT);
  107. }
  108.  
  109. /* -------- write a character to a window ------- */
  110. void wputch(WINDOW wnd, int c, int x, int y)
  111. {
  112.     if (CharInView(wnd, x, y))    {
  113.         hide_mousecursor();
  114.         poke(video_address,
  115.             vad(GetLeft(wnd)+x,GetTop(wnd)+y),(c & 255) |
  116.                 (clr(foreground, background) << 8));
  117.         show_mousecursor();
  118.     }
  119. }
  120.  
  121. /* ------- write a string to a window ---------- */
  122. void wputs(WINDOW wnd, void *s, int x, int y)
  123. {
  124.     int x1 = GetLeft(wnd)+x;
  125.     int x2 = x1;
  126.     int y1 = GetTop(wnd)+y;
  127.     if (x1 < SCREENWIDTH && y1 < SCREENHEIGHT && isVisible(wnd))    {
  128.         int *ln;
  129.         if ((ln = malloc(400)) != NULL)    {
  130.             int *cp1 = ln;
  131.             unsigned char *str = s;
  132.             int fg = foreground;
  133.             int bg = background;
  134.             int len;
  135.             int off = 0;
  136.             while (*str)    {
  137.                 if (*str == CHANGECOLOR)    {
  138.                     str++;
  139.                     foreground = (*str++) & 0x7f;
  140.                     background = (*str++) & 0x7f;
  141.                     continue;
  142.                 }
  143.                 if (*str == RESETCOLOR)    {
  144.                     foreground = fg & 0x7f;
  145.                     background = bg & 0x7f;
  146.                     str++;
  147.                     continue;
  148.                 }
  149.                    *cp1 = (*str & 255) | (clr(foreground, background) << 8);
  150.                 if (ClipString)
  151.                     if (!CharInView(wnd, x, y))
  152.                         *cp1 = peek(video_address, vad(x2,y1));
  153.                 cp1++;
  154.                 str++;
  155.                 x++;
  156.                 x2++;
  157.             }
  158.             foreground = fg;
  159.             background = bg;
  160.                len = (int)(cp1-ln);
  161.                if (x1+len > SCREENWIDTH)
  162.                    len = SCREENWIDTH-x1;
  163.  
  164.             if (!ClipString && !TestAttribute(wnd, NOCLIP))    {
  165.                 /* -- clip the line to within ancestor windows -- */
  166.                 RECT rc = WindowRect(wnd);
  167.                 WINDOW nwnd = GetParent(wnd);
  168.                 while (len > 0 && nwnd != NULL)    {
  169.                     if (!isVisible(nwnd))    {
  170.                         len = 0;
  171.                         break;
  172.                     }
  173.                     rc = subRectangle(rc, ClientRect(nwnd));
  174.                     nwnd = GetParent(nwnd);
  175.                 }
  176.                 while (len > 0 && !InsideRect(x1+off,y1,rc))    {
  177.                     off++;
  178.                     --len;
  179.                 }
  180.                 if (len > 0)    {
  181.                     x2 = x1+len-1;
  182.                     while (len && !InsideRect(x2,y1,rc))    {
  183.                         --x2;
  184.                         --len;
  185.                     }
  186.                 }
  187.             }
  188.             if (len > 0)    {
  189.                 hide_mousecursor();
  190.                 movedata(FP_SEG(ln), FP_OFF(ln+off),
  191.                     video_address, vad(x1+off,y1), len*2);
  192.                 show_mousecursor();
  193.             }
  194.             free(ln);
  195.         }
  196.     }
  197. }
  198.  
  199. /* --------- get the current video mode -------- */
  200. void get_videomode(void)
  201. {
  202.     videomode();
  203.     /* ---- Monochrome Display Adaptor or text mode ---- */
  204.     if (ismono())
  205.         video_address = 0xb000;
  206.     else
  207.         /* ------ Text mode -------- */
  208.         video_address = 0xb800 + video_page;
  209. }
  210.  
  211. /* --------- scroll the window. d: 1 = up, 0 = dn ---------- */
  212. void scroll_window(WINDOW wnd, RECT rc, int d)
  213. {
  214.     union REGS regs;
  215.     hide_mousecursor();
  216.     regs.h.cl = RectLeft(rc);
  217.     regs.h.ch = RectTop(rc);
  218.     regs.h.dl = RectRight(rc);
  219.     regs.h.dh = RectBottom(rc);
  220.     regs.h.bh = clr(WndForeground(wnd),WndBackground(wnd));
  221.     regs.h.ah = 7 - d;
  222.     regs.h.al = 1;
  223.     int86(VIDEO, ®s, ®s);
  224.     show_mousecursor();
  225. }
  226.  
  227.  
  228.